/**
 * Created by wangshaohua on 2018/1/13.
 */
moduleLearningMap.controller("LearningMapQuery", function (LearningMapTemplateResource, LanguageService, $scope, $log, $state, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        $scope.reset();
        pageOnLoad($scope);
        ConstantService.addState($state.current, true, LanguageService.language('学习地图管理'));
        $scope.currentPublishStatus = 'RELEASED';
        $scope.query(1);
    });

    $scope.reset = function () {
        $scope.queryPara = {}
    }

    $scope.unPublish = function (id) {
        Alert.confirm(function () {
            LearningMapTemplateResource.unPublish({}, {learningMapId: id}, function () {
                Alert.success();
                $scope.query(1);
            });
        })
    }

    $scope.onQueryKeyDown = function () {
        if (event.keyCode == 13) {
            $scope.query(1);
        }
    }

    $scope.queryLearningMapSpace = function (publishStatus) {
        $scope.currentPublishStatus = publishStatus;
        $scope.query(1)
    }

    $scope.query = function (pageNo) {
        //LoadingService.show();
        LearningMapTemplateResource.query({pageNo: pageNo - 1, pageSize: 10, publishStatus: $scope.currentPublishStatus}, $scope.queryPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.queryResult = queryResult.data;
            $scope.pagination.setPage(queryResult.data, pageNo);
            //LoadingService.hide();
        });
    };
    $scope.pagination = new Pagination();
    $scope.pagination.setPageClickFn($scope.query);

    $scope.openDetail = function (record) {
        $state.go("sz.learningMapDetail", {
            learningMapId: record.id
        });
    }

    $scope.addLearningMap = function () {
        $scope.learningMapDetail = {
            picture: LearningMapTemplateUtils.findLearningMap().picture
        };

        DialogService.open("addLearningMap", $scope, function () {
            if ($scope.currentLearningMapId) {
                $state.go("sz.learningMapDetail", {
                    learningMapId: $scope.currentLearningMapId
                });
            }
        });
    }
});